package org.ljy.lotterysystem.service.activitystatus.operater;

import org.ljy.lotterysystem.dao.dataobject.ActivityPrizeDO;
import org.ljy.lotterysystem.dao.mapper.ActivityPrizeMapper;
import org.ljy.lotterysystem.service.dto.ConvertActivityStatusDTO;
import org.ljy.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PrizeOperator extends AbstractActivityOperator {

    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;



    @Override
    public Integer sqquance() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO statusConvertDTO) {
        Long activityId = statusConvertDTO.getActivityId();
        Long prizeId = statusConvertDTO.getPrizeId();
        ActivityPrizeStatusEnum targetStatus = statusConvertDTO.getTargetPrizeStatus();
        if(activityId == null
                || prizeId == null || targetStatus == null) {
            return false;
        }
        ActivityPrizeDO activityPrizeDO = activityPrizeMapper.selectByAPId(activityId,prizeId);
        if(activityPrizeDO == null) {
            return false;
        }
        //判断传入状态是否一致
        if(activityPrizeDO.getStatus().equalsIgnoreCase(statusConvertDTO.getTargetPrizeStatus().name())) {
            return false;
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO statusConvertDTO) {
        Long activityId = statusConvertDTO.getActivityId();
        Long prizeId = statusConvertDTO.getPrizeId();
        ActivityPrizeStatusEnum targetStatus = statusConvertDTO.getTargetPrizeStatus();
        try {
            activityPrizeMapper.updateStatus(activityId,prizeId,targetStatus.name());
            return true;
        }catch (Exception e) {
            return false;
        }
    }
}
